#include "page_login.h"
#include "ui_page_login.h"
#include "ui_mainwindow.h"
#include<QCryptographicHash>
#include "admsql.h"
#include <QRandomGenerator>
#include "smtp.h"
#include <QRegularExpression>
#include <qmessagebox.h>
Page_login::Page_login(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Page_login)
{
    ui->setupUi(this);
    hideKey();

}

Page_login::~Page_login()
{
    delete ui;
}

void Page_login::on_btn_login_clicked()
{
    // 获取用户名和密码
    QString aname = ui->le_name->text().trimmed();
    QString password = ui->le_password->text().trimmed();

    if (aname.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this, tr("警告"), tr("用户名和密码不能为空！"));
        return;
    }

    AdmSql sql;
    AdmInfo adminInfo;

    qDebug() << "Attempting login with:" << aname << password;

    if (sql.LoginIsRight(aname, password, &adminInfo)) {
        qDebug() << "Login successful for admin:" << adminInfo.aname;
        QMessageBox::information(this, tr("成功"), tr("登录成功！"));
        emit sendLoginSuccess(adminInfo);
        this->hide();
    } else {
        qDebug() << "账号或密码错误";
        QMessageBox::warning(this, tr("登录失败"), tr("账号或密码错误，请重试！"));
    }
}

void Page_login::on_btn_to_enrol_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
    emit pagetoenrol();
}


void Page_login::on_btn_to_login_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
    // void pagetologin();
}
void Page_login::on_btn_enrol_clicked()
{
    // 获取用户输入
    QString aname = ui->le_name2->text().trimmed();
    QString email = ui->le_email2->text().trimmed();
    QString code = ui->l2_code2->text().trimmed();
    AdmSql sql;

    qDebug() << "Attempting registration with:" << aname << email << code;

    if (aname.isEmpty() || email.isEmpty() || code.isEmpty()) {
        QMessageBox::warning(this, tr("警告"), tr("所有字段都必须填写！"));
        return;
    }

    if (!isEmailValid(email)) {
        QMessageBox::warning(this, tr("警告"), tr("请正确填写邮箱地址！"));
        return;
    }

    if (sql.EmailExit(email)) {
        QMessageBox::warning(this, tr("警告"), tr("该邮箱已注册！"));
        return;
    }
    if (code != this->emailcode) {
        QMessageBox::warning(this, tr("警告"), tr("验证码不匹配，请检查后重试！"));
        return;
    }

    QString aid;
    while (true) {
        aid = create_adminId();
        if (!sql.IdIsExit(aid)) {
            break;
        }
    }

    AdmInfo a;
    a.aid = aid;
    a.aname = aname;
    a.email = email;
    QString hashedPassword = QCryptographicHash::hash(QString("123456").toUtf8(), QCryptographicHash::Sha256).toHex();
    a.password = hashedPassword;  // 存储哈希值

    if (sql.AddAdm(a)) {
        QMessageBox::information(this, tr("成功"), tr("注册成功！默认密码为123456"));
        ui->stackedWidget->setCurrentIndex(0);
    } else {
        QMessageBox::critical(this, tr("注册失败"), tr("注册过程中发生错误，请稍后再试！"));
    }
}

void Page_login::on_pb_get_code_clicked()
{
    QString email = ui->le_email2->text().trimmed();

    if (!isEmailValid(email)) {
        QMessageBox::warning(this, tr("警告"), tr("请正确填写邮箱地址！"));
        return;
    }

    Smtp smtp;
    QByteArray recvaddr = email.toLocal8Bit();
    this->emailcode = smtp.send(recvaddr);
    if (!this->emailcode.isEmpty()) {
        QMessageBox::information(this, tr("验证码发送"), tr("验证码已发送到您的邮箱，请查收！"));
    } else {
        QMessageBox::critical(this, tr("发送失败"), tr("验证码发送失败，请检查网络连接或稍后再试！"));
    }
}
void Page_login::on_pb_get_code2_clicked()
{
    QString email = ui->le_email->text().trimmed();

    if (!isEmailValid(email)) {
        QMessageBox::warning(this, tr("警告"), tr("请正确填写邮箱地址！"));
        return;
    }
    Smtp smtp;
    QByteArray recvaddr = email.toLocal8Bit();
    this->emailcode = smtp.send(recvaddr);
    if (!this->emailcode.isEmpty()) {
        QMessageBox::information(this, tr("验证码发送"), tr("验证码已发送到您的邮箱，请查收！"));
    } else {
        QMessageBox::critical(this, tr("发送失败"), tr("验证码发送失败，请检查网络连接或稍后再试！"));
    }
}
QString Page_login::create_adminId()
{
    QString code;
    for (int i = 0; i < 5; ++i) {
        code += QString::number(QRandomGenerator::global()->bounded(0, 10));
    }
    return code;
}

bool Page_login::isEmailValid(const QString &email)
{
    QRegularExpression emailRegex(R"([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})");
    QRegularExpressionMatch match = emailRegex.match(email);
    return match.hasMatch();
}

void Page_login::hideKey()
{
    ui->le_password->setEchoMode(QLineEdit::Password);  //隐藏密码
    ui->le_password2->setEchoMode(QLineEdit::Password);
    ui->le_password3->setEchoMode(QLineEdit::Password);
}

void Page_login::on_tb_eye_clicked(bool checked)
{
    if(checked){
        ui->le_password->setEchoMode(QLineEdit::Normal);
    }
    else {
        // ui->tb_eye->setText("Off");
        ui->le_password->setEchoMode(QLineEdit::Password);
    }
}


void Page_login::on_btn_reset_clicked()
{
    QString newPassword = ui->le_password2->text().trimmed();
    QString confirmPassword = ui->le_password3->text().trimmed();
    QString email = ui->le_email->text().trimmed(); // 获取邮箱
    if (newPassword.isEmpty() || confirmPassword.isEmpty() || email.isEmpty()) {
        QMessageBox::warning(this, tr("输入错误"), tr("输入不能为空！"));
        return;
    }
    // 验证新密码和确认密码是否匹配
    if (newPassword != confirmPassword) {
        QMessageBox::warning(this, tr("密码不匹配"), tr("新密码与确认密码不匹配！"));
        return;
    }
    // 更新管理员信息
    AdmSql sql;
    if (sql.resetPassword(email, newPassword)){
        QMessageBox::information(this, tr("成功"), tr("更新管理员信息成功！"));
        ui->stackedWidget->setCurrentIndex(0);
    } else {
        QMessageBox::critical(this, tr("失败"), tr("更新管理员信息失败！"));
    }
}
void Page_login::on_pb_exit_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}
void Page_login::on_btn_to_reset_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
}
